package com.example.demo.service.Impl;

import com.example.demo.config.LoginHandlerInterceptor;
import com.example.demo.mapper.ReincarnationlogMapper;
import com.example.demo.pojo.LogTest;
import com.example.demo.pojo.Reincarnationlog;
import com.example.demo.service.ReincarnationlogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ReincarnationlogServiceImpl implements ReincarnationlogService {
    @Autowired
    private ReincarnationlogMapper reincarnationlogMapper;
    @Autowired
    private LoginHandlerInterceptor loginHandlerInterceptor;
    //轮回数据列表
    @Override
    public PageInfo ReincarnationlogQuery(Integer page, Integer limit, Reincarnationlog reincarnationlog) {
        PageHelper.startPage(page,limit);
        List<Reincarnationlog> list = reincarnationlogMapper.ReincarnationlogQuery(reincarnationlog);
        PageInfo<Reincarnationlog> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    //轮回列表总数
    @Override
    public int ReincarnationlogCount() {
        return reincarnationlogMapper.ReincarnationlogCount();
    }

    //轮回数据添加
    public String ReincarnationlogAdd(Reincarnationlog reincarnationlog) {
        LogTest logTest = loginHandlerInterceptor.UserTokenLog();
        Integer id = reincarnationlogMapper.selectLifebookReincarnationid(reincarnationlog);
        if (id != null) {
            return "添加失败，该用户正在轮回";
        }else {
            reincarnationlog.setAid(logTest.getRole());
            reincarnationlog.setCreate_time(LocalDateTime.now());
            reincarnationlogMapper.updatelifebook(reincarnationlog);
            reincarnationlogMapper.ReincarnationlogAdd(reincarnationlog);
            return "添加成功";
        }
    }
    //轮回数据修改
    @Override
    public void ReincarnationlogUpdate(Reincarnationlog reincarnationlog) {
        reincarnationlog.setCreate_time(LocalDateTime.now());
        reincarnationlogMapper.ReincarnationlogUpdate(reincarnationlog);
    }
    //轮回数据删除
    @Override
    public void ReincarnationlogDelete(Integer id) {
        reincarnationlogMapper.ReincarnationlogDelete(id);
    }

}
